package com.geekteam.geometrychess.handler.event;

import java.util.ArrayList;

import com.geekteam.geometrychess.database.DataAccessor;
import com.geekteam.geometrychess.extension.ChessExtension;
import com.geekteam.geometrychess.room.entity.Player;
import com.geekteam.geometrychess.utils.Message;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.variables.SFSUserVariable;
import com.smartfoxserver.v2.entities.variables.UserVariable;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class JoinZoneEventHandler extends BaseServerEventHandler{

	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		ChessExtension ext = (ChessExtension) getParentExtension();
		User user = (User) event.getParameter(SFSEventParam.USER);
		Player player = DataAccessor.getInstance().getPlayerInfo(user.getName());
		if(player!=null){
			ISFSObject sfsObject = new SFSObject();
			sfsObject = player.toSFSObject();
			UserVariable userVariable = new SFSUserVariable("variables", sfsObject);
			userVariable.setHidden(false);
			ArrayList<UserVariable> listUserVariables = new ArrayList<UserVariable>();
			listUserVariables.add(userVariable);
			ext.getApi().setUserVariables(user, listUserVariables);
		}else{
			SFSErrorData error = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
			throw new SFSException(Message.USER_NOT_FOUND, error);
		}
	}

}
